﻿#region MIT
/*
Engine Of Evermore (http://code.google.com/p/engineofevermore/)
Copyright (c) 2008, 2009 Michael Woerister

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Drawing;
using EngineOfEvermore.Imaging;
using EngineOfEvermore.Common;
using System.Xml.Linq;

namespace EngineOfEvermore.Scene.Visual
{
    [XmlObjectSerializer ( typeof( StaticSpriteSerializer ) )]
    public class StaticSprite : AbstractSprite
    {
        #region Variables
        ImageMap _imageMap;
        int _frame;
        #endregion

        #region Properties
        [SerializerIgnore] // This is handle manually
        public ImageMap ImageMap
        {
            get { return _imageMap; }
            set
            { 
                _imageMap = value;

                if ( _frame >= _imageMap.FrameCount )
                {
                    _frame = 0;
                }

                _updateSize();
            }
        }

        public int Frame
        {
            get { return _frame; }
            set 
            {
                Debug.Assert( 
                    value >= 0 && ( _imageMap == null || _frame < _imageMap.FrameCount ) 
                    );

                _frame = value;
                _updateSize();
            }
        }
        #endregion

        #region Methods
        public override void Draw( System.Drawing.RectangleF visibleArea, IRenderer renderer )
        {
            if ( _imageMap == null || _imageMap.IsDisposed )
                return;

            if ( _frame < 0 || _frame >= _imageMap.FrameCount )
                return;

            renderer.ImageMap = _imageMap;
            renderer.FlipX = this.FlipX;
            renderer.FlipY = this.FlipY;
            renderer.Scale = this.Scale;
            renderer.Color = Color.White;

            if ( !_clippingActive )
            {
                if ( !_isRotated )
                {
                    renderer.DrawFrame( _frame, _aabb );
                }
                else
                {
                    renderer.Rotation = this.WorldRotation;
                    renderer.DrawFrame( _frame, this.WorldPosition );
                }
            }
            else
            {
                renderer.Rotation = this.WorldRotation;
                renderer.DrawFrame( _frame, this.WorldPosition, this.ClipArea );
            }
        }

        void _updateSize()
        {
            if ( _imageMap != null )
            {
                Size size = _imageMap.GetFrameSize( _frame );
                OriginalSize = new Vector2d( size.Width, size.Height );
            }
            else
            {
                OriginalSize = Vector2d.Zero;
            }
        }

        protected override VisualComponentChild _clone()
        {
            var clone = new StaticSprite();
            clone.ImageMap = this.ImageMap;
            clone.Frame = this.Frame;

            _copyAbstractSpriteStateTo( clone );

            return clone;
        }
        #endregion
    }
    
    public class StaticSpriteSerializer : IXmlObjectSerializer
    {
        public Object Obj { get; set; }

        public void getXml( XElement node, XmlSerialization serializer )
        {
            throw new NotImplementedException();
            // serializer.getXmlFromProperties( this.Obj, node ); 
            //            
            // var staticSprite = (StaticSprite) this.Obj;
            //            
            // // untested:
            // node.SetElementValue( "ImageMap", staticSprite.ImageMap.ResourceName );
        }
        
        public Object loadFromXml( XElement node, XmlSerialization serializer )
        {
            var sprite = new StaticSprite();
            
            var imageMapResourceName = node.Descendants( "ImageMap" ).Single().Value;
            sprite.ImageMap = Blue.ResourceManager.ResourceManager.GetResource<ImageMap>( imageMapResourceName );
            
            serializer.loadPropertiesFromXml( sprite, node );
            
            return sprite;
        }
    }
}
